package com.frame.auth.constant.enums;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @auther zhangwj
 * @date 2021/3/18 下午8:10
 */
public enum AuthGrantTypeEnum {

    AUTHORIZATION_CODE(1, "authorization_code"),
    PASSWORD(2, "password"),
    REFRESH_TOKEN(3, "refresh_token"),
    IMPLICIT(4, "implicit"),
    CLIENT_CREDENTIALS(5, "client_credentials");


    public static Set<String> getAuthGrantType(List<Integer> types) {
        Set<String> authGrantTypes = new HashSet<>();
        if (null == types || types.isEmpty()) {
            return authGrantTypes;
        }
        for (AuthGrantTypeEnum authGrantTypeEnum : AuthGrantTypeEnum.values()) {
            for (Integer type : types) {
                if (authGrantTypeEnum.getType().equals(type)) {
                    authGrantTypes.add(authGrantTypeEnum.getGrantType());
                }
            }
        }
        return authGrantTypes;
    }

    public static String getList(int... types) {
        List<Integer> typeList = new ArrayList<>(types.length);
        for (Integer type : types) {
            typeList.add(type);
        }
        return JSON.toJSONString(typeList);
    }

    public static String defaultType() {
        List<Integer> typeList = new ArrayList<>(1);
        typeList.add(4);
        return JSON.toJSONString(typeList);
    }


    AuthGrantTypeEnum(Integer type, String grantType) {
        this.type = type;
        this.grantType = grantType;
    }

    private Integer type;

    private String grantType;

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public String getGrantType() {
        return grantType;
    }

    public void setGrantType(String grantType) {
        this.grantType = grantType;
    }
}
